package org.tib.concurrent;

import java.math.BigInteger;
import java.util.HashMap;
import java.util.Map;

/**
 * A sample implmentation of a Processor.
 * This implementation serves as the concrete implementation of the
 * factoria calculation which is intended to run in a seperate thread.
 */
public class FactorialProcessor extends Processor<Map<String,Integer>,MathComputeResult> {
    Map<String,Integer> candidate;
    public static final String FACT_FIGURE = "factorial.figure";

    @Override
    public MathComputeResult executeTask() throws ConcurrentException{
        // This is where our logic goes in invoking what we want.
        Factorial fact = new Factorial();
        MathComputeResult resultObj = new MathComputeResult();
        if(candidate == null) {
            throw new ConcurrentException("Cannot have null values for computations!!");
        }
        else {
            Map<String,Number> resultMap = new HashMap<String,Number>();
            resultMap.put(MathComputeResult.COMPUTATION, 3);
            resultMap.put(MathComputeResult.RESULT, fact.computeFactorial(candidate.get(FACT_FIGURE)));
            resultObj.setResult(resultMap);
        }
        return resultObj;
    }

    @Override
    public Map<String,Integer> getParam() throws ConcurrentException {
        return this.candidate;
    }

    @Override
    public void setParam(Map<String,Integer>  param) {
        this.candidate = param;
    }
}
